package com.whz.governance.redislimiter.tool;

import com.whz.governance.redislimiter.command.AtomicIncreaseCommand;
import com.whz.governance.redislimiter.command.ScriptParams;
import lombok.experimental.UtilityClass;
import org.springframework.util.Assert;

/**
 * @author whz
 */
@UtilityClass
public class AtomicIncreaseUtils {
    private static final AtomicIncreaseCommand COMMAND = new AtomicIncreaseCommand();

    /**
     * 在超时时间seconds内从1开始自增计数,如超时则从1开始
     *
     * @param key     计数关键字
     * @param seconds 关键字超时时间秒数
     * @return 当前计数
     */
    public Long execute(String key, int seconds) {
        Assert.hasText(key, "key not null");
        Assert.isTrue(seconds > 0, "seconds > 0");
        ScriptParams params = ScriptParams.create()
                                          .addKey(key)
                                          .addArg(seconds + "");
        return COMMAND.execute(params);
    }
}